import pyjd # this is dummy in pyjs.

from pyjamas import Window
from pyjamas.HTTPRequest import HTTPRequest
from pyjamas.JSONService import JSONProxy

from pyjamas.ui.decoratorpanel import DecoratedTabPanel, DecoratorPanel
from pyjamas.ui.decoratorpanel import DecoratorTitledPanel
from pyjamas.ui.Button import Button
from pyjamas.ui.Label import Label
from pyjamas.ui.Grid import Grid
from pyjamas.ui.CellFormatter import CellFormatter
from pyjamas.ui.RowFormatter import RowFormatter
from pyjamas.ui.HTMLTable import HTMLTable
from pyjamas.ui.CheckBox import CheckBox
from pyjamas.ui.AbsolutePanel import AbsolutePanel
from pyjamas.ui.Button import Button
from pyjamas.ui.CheckBox import CheckBox
from pyjamas.ui.VerticalPanel import VerticalPanel
from pyjamas.ui.HorizontalPanel import HorizontalPanel
from pyjamas.ui.RadioButton import RadioButton
from pyjamas.ui.Button import Button
from pyjamas.ui.Label import Label
from pyjamas.ui.TabPanel import TabPanel
from pyjamas.ui import HasAlignment
from pyjamas.ui.Image import Image
from pyjamas.ui.VerticalPanel import VerticalPanel
from pyjamas.ui.RootPanel import RootPanel
from pyjamas.ui.HorizontalPanel import HorizontalPanel
from pyjamas.ui.HTML import HTML
from pyjamas.ui.Composite import Composite
from pyjamas.ui import MouseListener
from pyjamas.ui import Event

import pygwt
from pyjamas import JSONParser 


class GridWidget(AbsolutePanel):

    def __init__(self, wtf, tab):
        AbsolutePanel.__init__(self)

        if tab == 'Dashboard':
            self.dashboard()
        elif tab == 'Invoices':
            self.rock()
        elif tab == 'Clients':
            self.rock()
        elif tab == 'Company':
            self.company()
        elif tab == 'Users':
            self.users()
        else:
            Window.alert(tab)

    def users(self):
        
        self.g=Grid()
        self.g.resize(5, 5)

        self.g.setHTML(0, 0, "<strong>Username</strong>")
        self.g.setHTML(0, 1, "<strong>Password</strong>")
        self.g.setHTML(0, 2, "<strong>Owner</strong>")
        self.g.setHTML(0, 3, "<strong>Edit</strong>")
        self.g.setHTML(0, 4, "<strong>Delete</strong>")

        self.g.setHTML(1, 0, "andy@txm.net")
        self.g.setHTML(1, 1, "********")
        self.g.setWidget(1, 2, RadioButton("admin_user"))
        self.g.setWidget(1, 3, RadioButton("edit_user"))
        self.g.setWidget(1, 4, CheckBox("delete_user1"))

        self.g.setHTML(2, 0, "rock@txm.net")
        self.g.setHTML(2, 1, "********")
        self.g.setWidget(2, 2, RadioButton("admin_user"))
        self.g.setWidget(2, 3, RadioButton("edit_user"))
        self.g.setWidget(2, 4, CheckBox("delete_user2"))

        self.g.setHTML(3, 0, "finance@txm.net")
        self.g.setHTML(3, 1, "********")
        self.g.setWidget(3, 2, RadioButton("admin_user"))
        self.g.setWidget(3, 3, RadioButton("edit_user"))
        self.g.setWidget(3, 4, CheckBox("delete_user3"))

        self.g.setHTML(4, 0, "&nbsp;")
        self.g.setHTML(4, 1, "&nbsp;")
        self.g.setWidget(4, 2, Button("Set Admin"))
        self.g.setWidget(4, 3, Button("Edit User"))
        self.g.setWidget(4, 4, Button("Delete User"))

        self.g.setBorderWidth(2)
        self.g.setCellPadding(4)
        self.g.setCellSpacing(1)

        self.add(self.g)

    def company(self):
        
        self.g=Grid()
        self.g.resize(10, 2)

        self.g.setHTML(0, 0, "<strong>Company Name</strong>")
        self.g.setHTML(1, 0, "<strong>Primary User</strong>")
        self.g.setHTML(2, 0, "<strong>Address</strong>")
        self.g.setHTML(3, 0, "<strong>Postcode</strong>")
        self.g.setHTML(4, 0, "<strong>Country</strong>")
        self.g.setHTML(5, 0, "<strong>Telephone</strong>")
        self.g.setHTML(6, 0, "<strong>Fax</strong>")
        self.g.setHTML(7, 0, "<strong>Registered Company Number</strong>")
        self.g.setHTML(8, 0, "<strong>VAT Number</strong>")
        self.g.setHTML(9, 0, "<strong>&nbsp;</strong>")

        self.g.setHTML(0, 1, "TXM")
        self.g.setHTML(1, 1, "Andrew McGregor")
        self.g.setHTML(2, 1, "6 Pembroke Road, Clifton, Bristol")
        self.g.setHTML(3, 1, "BS8 3AX")
        self.g.setHTML(4, 1, "Country")
        self.g.setHTML(5, 1, "07940 22 33 11")
        self.g.setHTML(6, 1, "n/a")
        self.g.setHTML(7, 1, "123 456 78")
        self.g.setHTML(8, 1, "123 456 78")
        self.g.setWidget(9, 1, Button("Edit"))

        self.g.setBorderWidth(2)
        self.g.setCellPadding(4)
        self.g.setCellSpacing(1)

        self.add(self.g)

    def dashboard(self):
        
        self.g=Grid()
        self.g.resize(5, 5)
        self.g.setHTML(0, 0, "<b>Grid Test</b>")
        self.g.setBorderWidth(2)
        self.g.setCellPadding(4)
        self.g.setCellSpacing(1)

        self.add(self.g)

    def rock(self):

        self.page=0
        self.min_page=1
        self.max_page=10
        
        self.addb=Button("Next >", self)
        self.subb=Button("< Prev", self)
        
        self.g=Grid()
        self.g.resize(5, 5)
        self.g.setHTML(0, 0, "<b>Grid Test</b>")
        self.g.setBorderWidth(2)
        self.g.setCellPadding(4)
        self.g.setCellSpacing(1)
        
        self.updatePageDisplay()

        self.add(self.subb)
        self.add(self.addb)
        self.add(self.g)

    def onClick(self, sender):
        print sender
        if sender==self.addb:
            self.page+=1
        elif sender==self.subb:
            self.page-=1
        self.updatePageDisplay()
        

    def updatePageDisplay(self):
        if self.page<self.min_page: self.page=self.min_page
        elif self.page>self.max_page: self.page=self.max_page
        total_pages=(self.max_page-self.min_page) + 1
        
        self.g.setHTML(0, 4, "<b>page %d of %d</b>" % (self.page, total_pages))
        
        if self.page>=self.max_page:
            self.addb.setEnabled(False)
        else:
            self.addb.setEnabled(True)
            
        if self.page<=self.min_page:
            self.subb.setEnabled(False)
        else:
            self.subb.setEnabled(True)

        for y in range(1, 5):
            for x in range(5):
                self.g.setText(y, x, "%d (%d,%d)" % (self.page, x, y))




class AJAXLoader:
    def __init__(self, panel):
        self.panel = panel

    def onCompletion(self, text):
        self.panel.setMessage(text)

    def onError(self, text, code):
        self.panel.onError(text, code)

    def onTimeout(self, text):
        self.panel.onTimeout(text)

    #def onRemoteResponse(self, response, request_info):

class InvoiceSite:

    def onModuleLoad(self):

        self.Tabs = DecoratedTabPanel(Size=("800px", "100%"))

        self.Tabs.add( GridWidget(self, "Dashboard" ), "Dashboard" )
        self.Tabs.add( GridWidget(self, "Invoices" ), "Invoices" )
        self.Tabs.add( GridWidget(self, "Clients" ), "Clients" )
        self.Tabs.add( GridWidget(self, "Company" ), "Company" )
        self.Tabs.add( GridWidget(self, "Users" ), "Users" )

        self.Tabs.selectTab(4)

        dp = DecoratorTitledPanel("invoicesite.com", "bluetitle", "bluetitleicon",
                      ["bluetop", "bluetop2", "bluemiddle", "bluebottom"])

        dp.add(self.Tabs)
        RootPanel().add(dp)

        self.message = HTML()
        RootPanel().add(self.message)

        #if HTTPRequest().asyncGet('/rpc?action=GetCompany', None, None, AJAXLoader(self)):
        if HTTPRequest().asyncGet(None, None, '/rpc?action=GetCompany', AJAXLoader(self)):
          ok = True

        #t = JSONProxy.__init__(self, "/rpc?action=GetCompany")

    def setMessage(self, content):
        r = loads(content)
        self.message.setHTML("<pre>AJAX: %s</pre>" % r)
        
    def onError(self, text, code):
        Window.alert(text + "<br />" + code)


if __name__ == '__main__':

    pyjd.setup("./public/index.html")

    app = InvoiceSite()
    app.onModuleLoad()

    pyjd.run()

